package cn.itcast.mr.wordcount;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

/**
 * Author itcast
 * Date 2021/10/15 17:34
 * 此类主要用于将  map类和reduce类连接起来，并将数据写入到文件系统中
 */
public class WordcountMain {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //todo 1 获取 job的实例
        Job job = Job.getInstance();
        //todo 提交到集群需要配置的属性
        job.setJarByClass(WordcountMain.class);
        //todo 2 通过job设置输入的类型
        job.setInputFormatClass(TextInputFormat.class);
        //todo 3 设置输入的文本地址
        TextInputFormat.addInputPath(job,new Path(args[0]));
        //todo 4 设置job的map类
        job.setMapperClass(WordcountMapper.class);
        //todo 5 设置map类的输出key的类型
        job.setMapOutputKeyClass(Text.class);
        //todo 6 设置map类的输出value的类型
        job.setMapOutputValueClass(IntWritable.class);
        //todo 7 设置reduce类
        job.setReducerClass(WordcountReducer.class);
        //todo 7 设置reduce类的输出key的类型
        job.setOutputKeyClass(Text.class);
        //todo 8 设置reduce类的输出value的类型
        job.setOutputValueClass(IntWritable.class);
        //todo 9 设置输出的类型
        job.setOutputFormatClass(TextOutputFormat.class);
        //todo 10 设置输出的文件路径
        TextOutputFormat.setOutputPath(job,new Path(args[1]));
        //todo 11 执行任务等待结束
        boolean flag = job.waitForCompletion(true);
        //todo 12 执行成功给个 0 ；否则给个 1
        System.exit(flag?0:1);
    }

}
